package persentation.presentTeacher;

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.imageio.ImageTypeSpecifier;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.text.ComponentView;

import org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper;

import persentation.presentLogin.LoginFrame;

import po.userPO.UserPO;
import dataservice.ResultMessage;
import dataservice.userDataService.UserDatabaseService;
import businesslogic.chooseCourseBl.ChooseCourseController;
import businesslogic.courseBl.CourseController;
import businesslogic.informBl.InformController;
import businesslogic.userBl.TeacherController;

import java.rmi.Naming;
import java.rmi.RMISecurityManager;
import java.awt.Color;

import javax.swing.JScrollPane;
import javax.swing.SwingConstants;

public class TeacherFrame extends JFrame {

	/**
	 * 
	 */
	
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private Title title;
	private ButtonPanel buttonPanel;
	private CourseController courseController;
	private TeacherController teacherController;
	private ChooseCourseController chooseCourseController;
	private MyTabbedPane tabbedPane;
	
	private AddCourseInfoPanel addCourseInfoPanel;
	private ShowStudentListPanel showStudentListPanel;
	private ChangePasswordPanel changePasswordPanel;
	private UploadScorePanel uploadScorePanel;
	private InformPanel informPanel;

	private InformController informController;
	private ImageIcon courseIcon;
	private ImageIcon passwordIcon;
	private ImageIcon scoreIcon;
	private ImageIcon courseInfoIcon;
	private ImageIcon studentIcon;
	
	private int tabNum=0;
	private JLabel courseIconLabel;
	private JLabel passwordIconLabel;
	private JLabel scoreIconLabel;
	private JLabel studentIconLabel;
	private JLabel buttonBottomlLabel;
	
	private BottomPanel bottomPanel;
	
	
	private String userName;

	public static void main(String[] args) {

		UIManager.put("RootPane.setupButtonVisible", false);
		BeautyEyeLNFHelper.translucencyAtFrameInactive = false;
		try {

			org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper.launchBeautyEyeLNF();
		} catch (Exception e) {
			e.printStackTrace();
		}

		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					TeacherFrame frame = new TeacherFrame("T00001");
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public TeacherFrame(String ID) {
		
		
		courseController = new CourseController(ID);
		teacherController = new TeacherController(ID);
		chooseCourseController=new ChooseCourseController(ID);

		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setResizable(false);
		setTitle("南京大学选课系统");
		setSize(1100, 750);
		Toolkit toolkit = Toolkit.getDefaultToolkit();
		Dimension screen = toolkit.getScreenSize();
		int x = (int) (screen.getWidth() - this.getWidth()) >> 1;
		int y = (((int) ((screen.getHeight() - this.getHeight()) - 15) >> 1));
		setLocation(x, y);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

//		if (System.getSecurityManager() == null)
//			System.setSecurityManager(new RMISecurityManager());
//		try {
//
//			UserDatabaseService userDataService = (UserDatabaseService) Naming
//					.lookup("rmi://localhost:8888/userData");
//			UserPO findResult = userDataService.find(ID);
//			userName = findResult.getName();
//		} catch (Exception exception) {
//			exception.printStackTrace();
//		}
		
		try {
			userName=teacherController.getUserName(ID);
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			ExceptionHandler.networkException();
		}

		
		
		bottomPanel=new BottomPanel(userName);
		
		tabbedPane = new MyTabbedPane();
		tabbedPane.setBounds(220, 82, 820, 629);
		contentPane.add(tabbedPane);
		tabbedPane.addBottom(bottomPanel);
		tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
		title = new Title(userName,ID,tabbedPane);
		title.setFrame(this);
//		title.setTabbedPane(tabbedPane);
		
		title.setSize(1100, 83);
		contentPane.add(title);
		title.setLocation(0, 0);
		tabbedPane.addMouseListener(new MouseListener() {
			
			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseClicked(MouseEvent e) {
				
				
				
			}
		});
		
		tabbedPane.addAncestorListener(new AncestorListener() {
			
			@Override
			public void ancestorRemoved(AncestorEvent event) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void ancestorMoved(AncestorEvent event) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void ancestorAdded(AncestorEvent event) {
				// TODO Auto-generated method stub
				
			}
		});
		
//		tabbedPane.addMouseListener(new ActionListener() {
//			public void actionPerformed(ActionEvent e) {
//				if (e.getActionCommand().equals("ON_TAB_CLOSE")) {
//					int index = tabbedPane.getSelectedIndex();
//					tabbedPane.removeTabAt(index);
//					tabbedPane.removeOne(index);
//					if (tabbedPane.getTabCount()==0) {
//						tabbedPane.addOne("欢迎使用",bottomPanel);
//					}
//				}
//				;
//
//			}
//		});
		tabbedPane.addOne("欢迎使用", bottomPanel);
		
		tabbedPane.addChangeListener(new ChangeListener() {
			
			@Override
			public void stateChanged(ChangeEvent e) {
				if (tabbedPane.getSelectedComponent()==addCourseInfoPanel ) {
					addCourseInfoPanel.fillTable();
					
				}
				if(tabbedPane.getSelectedComponent()==showStudentListPanel){
					showStudentListPanel.fillTable();
				}
//				tabbedPane.updateUI();
//				if(tabbedPane.getTabCount()==0)
//					tabbedPane.addOne("欢迎使用", bottomPanel);
				
				
				
			}
			
		}); 
		
		
		tabbedPane.addComponentListener(new ComponentListener() {
			
			@Override
			public void componentShown(ComponentEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void componentResized(ComponentEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void componentMoved(ComponentEvent e) {
				tabbedPane.updateUI();
			
					
				
			}
			
			@Override
			public void componentHidden(ComponentEvent e) {
				// TODO Auto-generated method stub
				
			}
		});
		

//		showCoursePanel = new ShowCoursePanel(courseController);
//		showCoursePanel.setTabbedPane(tabbedPane);
		showStudentListPanel = new ShowStudentListPanel(courseController,chooseCourseController);
		showStudentListPanel.setTabbedPane(tabbedPane);
		addCourseInfoPanel = new AddCourseInfoPanel(courseController);
		addCourseInfoPanel.setTabbedPane(tabbedPane);
		uploadScorePanel = new UploadScorePanel(courseController,
				chooseCourseController);
		uploadScorePanel.setTabbedPane(tabbedPane);
		changePasswordPanel = new ChangePasswordPanel(ID);
		changePasswordPanel.setTabbedPane(tabbedPane);

		

		buttonPanel = new ButtonPanel();
		buttonPanel.setBackground(Color.WHITE);
		buttonPanel.setBounds(0, 64, 200, 657);
		contentPane.add(buttonPanel);
		

		JButton button_3 = new JButton("         修改密码");
		button_3.setBounds(10, 271, 170, 50);
		buttonPanel.add(button_3,0);
		button_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				tabbedPane.addOne("修改密码", changePasswordPanel);

			}
		});
		button_3.setFont(new Font("微软雅黑", Font.BOLD, 20));
		
		passwordIcon=new ImageIcon("Graphics/teacherGraphics/password.png");
		passwordIconLabel=new JLabel(passwordIcon);
		passwordIconLabel.setBounds(0, 294, 60, 60);
		button_3.add(passwordIconLabel);
		

		JButton button_2 = new JButton("     登记课程成绩");
		button_2.setBounds(10, 197, 170,50);
		buttonPanel.add(button_2,0);
		button_2.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				tabbedPane.addOne("登记成绩", uploadScorePanel);
			}
		});
		button_2.setFont(new Font("微软雅黑", Font.BOLD, 20));
		
		studentIcon=new ImageIcon("Graphics/teacherGraphics/score.png");
		studentIconLabel=new JLabel(studentIcon);
		studentIconLabel.setBounds(0,233,60,60);
		button_2.add(studentIconLabel);

		JButton button_1 = new JButton("     查看学生列表");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				tabbedPane.addOne("查看学生列表", showStudentListPanel);
			}
		});
		button_1.setBounds(10, 121, 170,50);
		buttonPanel.add(button_1,0);
		button_1.setFont(new Font("微软雅黑", Font.BOLD, 20));
		
		studentIcon=new ImageIcon("Graphics/teacherGraphics/student.png");
		studentIconLabel=new JLabel(studentIcon);
		studentIconLabel.setBounds(0,152, 60,60);
		button_1.add(studentIconLabel);
		
		courseInfoIcon=new ImageIcon("Graphics/teacherGraphics/courseInfo.png");
		
		JButton btnNewButton = new JButton("         我的课程");
		btnNewButton.setBounds(10, 43, 170,50);
		buttonPanel.add(btnNewButton,0);
		btnNewButton.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
				tabbedPane.addOne("我的课程", addCourseInfoPanel);
			}
		});
				
		btnNewButton.setFont(new Font("微软雅黑", Font.BOLD, 20));

		courseIcon=new ImageIcon("Graphics/teacherGraphics/course.png");
		courseIconLabel=new JLabel(courseIcon);
		courseIconLabel.setBounds(0, 10, 50, 60);
		btnNewButton.add(courseIconLabel);
				
		new ExceptionHandler(this, new LoginFrame());
	
	}
	
	
	
	
	
	
}
